﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace SlimCode.Utils
{
  public abstract class KeyedHashAlgorithm : HashAlgorithm
  {
    protected KeyedHashAlgorithm()
    {
    }

    #region Key PROPERTY

    protected byte[] m_key = null;

    public virtual byte[] Key
    {
      get 
      {
        if( m_key == null )
          return null;

        return ( byte[] )m_key.Clone(); 
      }
      set
      {
        if( this.State == 0 )
          throw new CryptographicException( "Cannot set key after hashing has started." );

        if( value == null )
          m_key = null;
        else
          m_key = ( byte [] )value.Clone();
      }
    }

    #endregion

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( m_key != null )
        {
          Array.Clear( m_key, 0, m_key.Length );
          m_key = null;
        }
      }

      base.Dispose( disposing );
    }
  }
}
